package schoolword;

import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        //简单
        Scanner sc = new Scanner(System.in);
        while (true) {  //写个死循环
            System.out.println("请输入一个邮箱: ");
            String str = sc.nextLine();
            if (checkEmail(str)) {
                String email = getDomain(str);
                System.out.println("该邮箱地址的域名是: " + email);
                break;
            }else {
                System.out.println("请重新输入");
            }
        }
    }

    private static boolean checkEmail(String s) {
        if (!s.contains("@")) {
            return false;
        }
        int index1 = s.indexOf("@");
        if (index1 == 0 || index1 == s.length()-1) {
            return false;
        }
        String str1 = s.substring(index1+1);
        if (!str1.contains(".")) {
            return false;
        }
//        int index2 = str1.indexOf(".");
//        if (index2 == 0 || index2 == str1.length()-1) {
//            return false;
//        }
        String str3 = s.substring(0,index1);
        if (str3.length() < 3) {
            return false;
        }
        if(!s.endsWith(".com") || !s.endsWith(".org") || !s.endsWith(".cn") || !s.endsWith(".net")){
            return false;
        }
        return true;
    }
    private static String getDomain(String s) {
        int aIndex = s.indexOf("@");
        int bIndex = s.indexOf(".",aIndex+1);
        return s.substring(aIndex+1,bIndex);
    }
}
